package com.codingvip;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class RedisCluster {


    /**
     * 访问redis集群
     *
     * @author 图灵-诸葛老师
     */
    public static void main(String[] args) throws IOException {
        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.95.125", 8001));
        jedisClusterNode.add(new HostAndPort("192.168.95.126", 8002));
        jedisClusterNode.add(new HostAndPort("192.168.95.52", 8003));
        jedisClusterNode.add(new HostAndPort("192.168.95.125", 8004));
        jedisClusterNode.add(new HostAndPort("192.168.95.126", 8005));
        jedisClusterNode.add(new HostAndPort("192.168.95.52", 8006));

        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxTotal(10);
        config.setMaxIdle(10);
        config.setTestOnBorrow(true);
        //connectionTimeout：指的是连接一个url的连接等待时间
        //soTimeout：指的是连接上一个url，获取response的返回等待时间
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNode, 16000, 5000, 10, "songtao", config);
        System.out.println(jedisCluster.set("student", "zhuge"));
        System.out.println(jedisCluster.set("age", "19"));

        System.out.println(jedisCluster.get("student"));
        System.out.println(jedisCluster.get("age"));

        jedisCluster.close();
    }

}
